using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;

namespace BitMango.Diagnostics
{
    public static class Debugger
    {
        static Debugger()
        {
            TraceListener.Init(new Hashtable
            {
                {
                    "store_type",
                    "GoogleStore"
                },
                {
                    "device_model",
                    SystemInfo.deviceModel
                }
            });
        }

        public static bool EnableLocalLog
        {
            get
            {
                return TraceListener.enableLocalLog;
            }
            set
            {
                TraceListener.enableLocalLog = value;
            }
        }

        public static void SetADID(string adid)
        {
            TraceListener.AddDefaultTable("adid", adid);
        }

        private static string BuildStackTrace(int skipLine)
        {
            string text = StackTraceUtility.ExtractStackTrace();
            while (skipLine > 0)
            {
                int num = text.IndexOf(Environment.NewLine, 0);
                if (num != -1)
                {
                    text = text.Substring(num + 1);
                }
                skipLine--;
            }
            return text;
        }

        public static void Assert(bool condition)
        {
            if (condition)
            {
                return;
            }
            if (Platform.IsEU() && !Platform.IsTOSAgreed)
            {
                return;
            }
            string str = Debugger.BuildStackTrace(2);
            UnityEngine.Debug.LogException(new Exception(Debugger.assertPrefix + str));
        }

        public static void Assert(bool condition, string message)
        {
            if (condition)
            {
                return;
            }
            if (Platform.IsEU() && !Platform.IsTOSAgreed)
            {
                return;
            }
            message = message.Replace(" ", "_");
            string text = Debugger.BuildStackTrace(2);
            UnityEngine.Debug.LogException(new Exception(string.Concat(new object[]
            {
                Debugger.assertPrefix,
                message,
                '\n',
                text
            })));
        }

        public static void Log(string message)
        {
            if (Debugger.EnableLocalLog)
            {
                UnityEngine.Debug.Log(message);
            }
            if (Platform.IsEU() && !Platform.IsTOSAgreed)
            {
                return;
            }
        }

        public static void LogWarning(string message)
        {
            UnityEngine.Debug.LogWarning(message);
            if (Platform.IsEU() && !Platform.IsTOSAgreed)
            {
                return;
            }
        }

        public static void LogError(string message)
        {
            UnityEngine.Debug.LogError(message);
            if (Platform.IsEU() && !Platform.IsTOSAgreed)
            {
                return;
            }
        }

        public static void AssertInspector(MonoBehaviour mono)
        {
        }

        internal static readonly string assertPrefix = "[ASSERT]_";

        public class Application
        {
            //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
            private static event UnityEngine.Application.LogCallback logCallback;

            public static void RegisterLogCallback(UnityEngine.Application.LogCallback handler, bool register = true)
            {
                if (handler == null)
                {
                    UnityEngine.Debug.LogError("BitMango.Diagnostics.Debugger.Application::RegisterLogCallback - invalid handle, register : " + register.ToString());
                    return;
                }
                if (register)
                {
                    Debugger.Application.logCallback += handler;
                }
                else
                {
                    Debugger.Application.logCallback -= handler;
                }
                UnityEngine.Application.RegisterLogCallback(Debugger.Application.logCallback);
            }
        }
    }
}
